﻿@model TestOnlineProject.Models.Entities.User
@{
    ViewBag.Title = "Change";
}
<script>
    $(document).ready(function () {
        $("button[name=submit]").click(function () {
            var oldPass = $("#Password").val();
            var newPass = $("#NewPassword").val();
            var confirm = $("#ReEnterPassword").val();
            $("#OldPassword_Message").html("");
            $("#NewPassword_Message").html("");
            $("#ReEnterPassword_Message").html("");

            if (oldPass == "") {
                $("#OldPassword_Message").html("Old Password is required!");
            } else if (newPass == "" || newPass == oldPass) {
                $("#OldPassword_Message").html("");
                $("#NewPassword_Message").html("New Password must be different with Old Password!");
                return false;
            } else if (confirm != newPass) {
                $("#OldPassword_Message").html("");
                $("#NewPassword_Message").html("");
                $("#ReEnterPassword_Message").html("Confirm New Password must be equal New Password!");
            } else {
                var con = confirm("Are your sure change Your Password?");
                if (con == true) {
                    $.ajax({
                        url: '@Url.Action("ChangePassword")',
                        type: 'POST',
                        data: $("#form").serialize(),
                        success: function (result) {
                            if (result == 2) {
                                alert("Password change successfully!");
                                window.location = '/TestOnlineProject/Account';
                            } else if (result == 1) {
                                alert("Your account has been Inactive. Please contact admin to have more information!");
                                window.location = '/TestOnlineProject/Account/Logout';
                            } else {
                                alert("Your old Password is wrong. Please try again!");
                            }
                        }
                    });
                }
            }
        });
    });
</script>
<div class="site_map">
    @Html.ActionLink("Home", "Index", "Home", new { }, new { }) / Account
</div>
<div>
    <br />
    <div class="tabbable tabs-left">
        <ul class="nav nav-tabs">
            <li>
                @Html.ActionLink("Overview", "Index", "Account", new { userId = @Model.UserId }, null)
            </li>
            @if (!HttpContext.Current.User.IsInRole("1"))
            {
                <li>
                    @Html.ActionLink("Subjects", "Subject", "Account", new { userId = @Model.UserId }, null)
                </li>
            }
            <li>
                @Html.ActionLink("Discussions", "Discussion", "Account", new { userId = @Model.UserId }, null)
            </li>
            @if (Model.UserId == int.Parse(HttpContext.Current.User.Identity.Name))
            {
                <li class="active"><a data-toggle="tab" href="#changePass">Password</a> </li>
            }
        </ul>
        <div class="tab-content">
            <div id="changePass" class="tab-pane active">
                <h3 style="color: #A5CAE4; font-size: 16px; margin-bottom: 6px; border-bottom: 1px solid #D7EDFC;
                                      line-height: 1.28">
                    Change Password
                </h3>
                @using (Html.BeginForm("ChangePassword", "Account", FormMethod.Post, new { id = "form" }))
                {
                    @Html.HiddenFor(model => model.UserId)
                    @Html.HiddenFor(model => model.Username)
                    <table class="table">
                        <tr>
                            <td style="text-align: right; padding-right: 30px; border-top: none; border-bottom: 1px solid #D7EDFC; width: 30%">
                               Old Password (*)
                            </td>
                            <td style="text-align: left; border-top: none; border-bottom: 1px solid #D7EDFC">
                                @Html.PasswordFor(model => model.Password)
                                <br />
                                <span class="errorMsg" id="OldPassword_Message"></span>
                            </td>
                        </tr>
                        <tr>
                            <td style="text-align: right; padding-right: 30px; border-top: none">
                                New Password (*)
                            </td>
                            <td style="text-align: left; border-top: none">
                                <input type="password" id="NewPassword" value="" name="NewPassword" />
                                <br />
                                <span class="errorMsg" id="NewPassword_Message"></span>
                            </td>
                        </tr>
                        <tr>
                            <td style="text-align: right; padding-right: 30px; border-top: none; border-bottom: 1px solid #D7EDFC">
                                Confirm new Password (*)
                            </td>
                            <td style="text-align: left; border-top: none; border-bottom: 1px solid #D7EDFC">
                                <input type="password" id="ReEnterPassword" value="" name="ReEnterPassword" />
                                <br />
                                <span class="errorMsg" id="ReEnterPassword_Message"></span>
                            </td>
                        </tr>
                        <tr>
                            <td style="text-align: right; padding-right: 30px">
                                <button class="btn" type="button" name="submit">Save</button>
                            </td>
                            <td style="text-align: left">
                                @Html.ActionLink("Reset", "ChangePassword", "Account", new { @class = "btn" })
                            </td>
                        </tr>
                    </table>
                }
            </div>
        </div>
    </div>
</div>